#include "login.h"
#include "com/virt_kb/virt_kb.h"
#include "ui/bar/status_bar/status_bar.h"

// 文本框事件回调函数
static void ta_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *ta = lv_event_get_target(e);
    virt_kb_t *kb = lv_event_get_user_data(e);
    
    if (code == LV_EVENT_FOCUSED) {
        LV_LOG_USER("文本框获得焦点，显示虚拟键盘");
        if (kb) {
            virt_kb_show(kb, ta);
        }
    } else if (code == LV_EVENT_DEFOCUSED) {
        LV_LOG_USER("文本框失去焦点，隐藏虚拟键盘");
        if (kb) {
            virt_kb_hide(kb);
        }
    }
}

// 显示错误消息
static void show_error_message(const char *message)
{
    lv_obj_t *mbox = lv_msgbox_create(NULL, "ERROR", message, NULL, true);
    lv_obj_center(mbox);
}

// 定时器回调函数，用于恢复密码隐藏
static void hide_password_timer_cb(lv_timer_t *timer)
{
    login_t *login = timer->user_data;
    if (login && login->show_pwd_label) {
        lv_label_set_text(login->show_pwd_label, LV_SYMBOL_EYE_CLOSE);
    }
    if (login && login->password_input) {
        lv_textarea_set_password_mode(login->password_input, true);
    }
}

// 显示密码按钮的事件回调函数
static void show_pwd_btn_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    login_t *login = lv_event_get_user_data(e);
    if(code == LV_EVENT_CLICKED && login) {
        // 显示密码
        lv_textarea_set_password_mode(login->password_input, false);
        // 更改显示密码按钮的文本
        if (login->show_pwd_label) {
            lv_label_set_text(login->show_pwd_label, LV_SYMBOL_EYE_OPEN);
        }
        
        // 5秒后恢复密码隐藏
        lv_timer_t *timer = lv_timer_create(hide_password_timer_cb, 1000, login);
        lv_timer_set_repeat_count(timer, 1);
    }
}

// 登录按钮事件回调函数
static void login_btn_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    login_t *login = lv_event_get_user_data(e);
    
    if(code == LV_EVENT_CLICKED && login) {
        const char *username = lv_textarea_get_text(login->username_input);
        const char *password = lv_textarea_get_text(login->password_input);

        if (login->verify_cb && login->verify_cb(username, password)) {
            
        } else {
            show_error_message("username or password error！");
        }
    }
}

// 创建滚动容器
static lv_obj_t* create_scroll_container(lv_obj_t *parent)
{
    lv_obj_t *container = lv_obj_create(parent);
    if (!container) {
        return NULL;
    }

    lv_obj_set_size(container, lv_pct(100), lv_pct(90));
    lv_obj_align(container, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_set_style_border_width(container, 0, 0);
    lv_obj_set_style_pad_all(container, 30, 0);
    lv_obj_set_style_pad_top(container, 50, 0);
    lv_obj_set_scrollbar_mode(container, LV_SCROLLBAR_MODE_AUTO);
    lv_obj_set_scroll_dir(container, LV_DIR_VER);
    lv_obj_set_flex_flow(container, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(container, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_row(container, 30, 0);
    lv_obj_clear_flag(container, LV_OBJ_FLAG_SCROLL_CHAIN_HOR);
    lv_obj_add_flag(container, LV_OBJ_FLAG_SCROLL_ONE);

    return container;
}

// 创建用户名输入区域
static void create_username_area(login_t *login, lv_obj_t *parent)
{
    // 创建用户名输入区域的容器
    lv_obj_t *username_container = lv_obj_create(parent);
    lv_obj_set_size(username_container, 320, 40);
    lv_obj_set_style_pad_all(username_container, 0, 0);
    lv_obj_set_style_border_width(username_container, 0, 0);
    lv_obj_set_flex_flow(username_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(username_container, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    // 创建用户名标签
    lv_obj_t *username_label = lv_label_create(username_container);
    lv_label_set_text(username_label, "User name");
    lv_obj_set_style_min_width(username_label, 80, 0);

    // 创建用户名输入框
    login->username_input = lv_textarea_create(username_container);
    lv_textarea_set_text(login->username_input, "admin");
    lv_obj_set_size(login->username_input, 200, 40);
    lv_obj_add_event_cb(login->username_input, ta_event_cb, LV_EVENT_ALL, login->kb);
}

// 创建密码输入区域
static void create_password_area(login_t *login, lv_obj_t *parent)
{
    // 创建密码输入区域的容器
    lv_obj_t *password_container = lv_obj_create(parent);
    lv_obj_set_size(password_container, 320, 40);
    lv_obj_set_style_pad_all(password_container, 0, 0);
    lv_obj_set_style_border_width(password_container, 0, 0);
    lv_obj_set_flex_flow(password_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(password_container, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    // 创建密码标签
    lv_obj_t *password_label = lv_label_create(password_container);
    lv_label_set_text(password_label, "Password");
    lv_obj_set_style_min_width(password_label, 80, 0);

    // 创建密码输入框
    login->password_input = lv_textarea_create(password_container);
    lv_textarea_set_placeholder_text(login->password_input, "Password");
    lv_textarea_set_password_mode(login->password_input, true);
    lv_obj_set_size(login->password_input, 200, 40);
    lv_obj_add_event_cb(login->password_input, ta_event_cb, LV_EVENT_ALL, login->kb);
    lv_obj_clear_flag(login->password_input, LV_OBJ_FLAG_SCROLLABLE);

    // 创建显示密码按钮
    lv_obj_t *show_pwd_btn = lv_btn_create(login->password_input);
    lv_obj_set_size(show_pwd_btn, 40, 40);
    lv_obj_align(show_pwd_btn, LV_ALIGN_RIGHT_MID, 10, 0);
    lv_obj_set_style_bg_opa(show_pwd_btn, LV_OPA_0, 0);
    lv_obj_set_style_border_width(show_pwd_btn, 0, 0);
    lv_obj_set_style_shadow_width(show_pwd_btn, 0, 0);
    lv_obj_set_style_pad_all(show_pwd_btn, 0, 0);
    login->show_pwd_label = lv_label_create(show_pwd_btn);
    lv_label_set_text(login->show_pwd_label, LV_SYMBOL_EYE_CLOSE);
    lv_obj_set_style_border_width(login->show_pwd_label, 0, 0);
    lv_obj_set_style_text_color(login->show_pwd_label, lv_color_black(), 0);
    lv_obj_center(login->show_pwd_label);
    lv_obj_add_event_cb(show_pwd_btn, show_pwd_btn_event_cb, LV_EVENT_CLICKED, login);
}

// 创建登录按钮
static void create_login_button(login_t *login, lv_obj_t *parent)
{
    lv_obj_t *btn = lv_btn_create(parent);
    lv_obj_set_size(btn, 120, 40);
    lv_obj_add_event_cb(btn, login_btn_event_cb, LV_EVENT_CLICKED, login);

    lv_obj_t *btn_label = lv_label_create(btn);
    lv_label_set_text(btn_label, "Login");
    lv_obj_center(btn_label);
}

login_t* login_create(lv_obj_t *parent, verify_callback_t verify_cb, virt_kb_t *kb)
{
    if (!parent) {
        return NULL;
    }

    login_t *login = (login_t *)lv_mem_alloc(sizeof(login_t));
    if (!login) {
        return NULL;
    }

    // 初始化结构体
    login->container = NULL;
    login->username_input = NULL;
    login->password_input = NULL;
    login->show_pwd_label = NULL;
    login->kb = kb;
    login->verify_cb = verify_cb;

    // 创建滚动容器
    login->container = create_scroll_container(parent);
    if (!login->container) {
        lv_mem_free(login);
        return NULL;
    }

    // 创建各个UI组件
    create_username_area(login, login->container);
    create_password_area(login, login->container);
    create_login_button(login, login->container);

    return login;
}

void login_destroy(login_t *login)
{
    if (login) {
        if (login->container) {
            lv_obj_del(login->container);
        }
        lv_mem_free(login);
    }
}